package ArrayPackage;

import org.junit.jupiter.api.Test;

/**
 * @author Lzm
 * @version 1.0
 */
public class search_ {

  @Test
  public void searchTest(){
      int[] nums = new int[]{-1,0,3,5,9,12};
      int target = 2;
    System.out.println(search(nums,target));
  }


  public int search(int[] nums, int target) {
      if (nums[nums.length - 1] < target || nums[0] > target){
        return -1;
      }
      int left = 0 ;
      int right = nums.length - 1;
      while(left <= right){
        int middle = ((right - left)/2 + left);
        if (nums[middle] == target){
          return middle;
        }
        else if(nums[middle] > target){
          right = middle - 1;
        }
        else if(nums[middle] < target){
          left = middle + 1;
        }
      }
      return -1;
  }
}
